package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.salesmanage.util.OrganizeCustom;
import com.turing.post.salesmanage.util.OrganizeCustomBusiness;
import com.turing.post.salesmanage.util.OrganizeCustomDao;
import com.turing.post.salesmanage.util.RiskLevelChangeDetail;
import com.turing.post.salesmanage.util.RiskLevelChangeDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "salesmanage.impl.OrganizeCustomBusinessImpl")
@Remote
@Local
public class OrganizeCustomBusinessImpl implements OrganizeCustomBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	
	@EJB(mappedName = "salesmanage.impl.OrganizeCustomDaoImpl")
	private OrganizeCustomDao organizeCustomDaoImpl;

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "salesmanage.impl.RiskLevelChangeDetailDaoImpl")
	private RiskLevelChangeDetailDao riskLevelChangeDetailDaoImpl;

	@Override
	public BigInteger insertOrganizeCustomInfo(PCPSWebInput webInput,
			OrganizeCustom organizeCustom) throws PCPSApplicationException {
		// 对象
		if (organizeCustom == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL, "单位信息输入为NULL");
		}
		// 发卡商
		if (PCPSUtil.isEmpty(organizeCustom.getIssueCompanyNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
					"IssueCompanyNo");
		}
		// 地区编码
		if (PCPSUtil.isEmpty(organizeCustom.getNationCodeId())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
					"NationCodeId");
		}
		// 单位名称
		if (PCPSUtil.isEmpty(organizeCustom.getFullName())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL, "FullName");
		}
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			organizeCustom.setIssueCompanyNo(issueCompanyNo);
		} else {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
					"归属机构号为空，无法添加自己归属下信息!");
		}
		// 获取个人客户信息主键
		BigInteger organizeId = commonDaoImpl.getUniqueIntegerKey();
		organizeCustom.setOrganizeId(organizeId);
		// 新增操作员
		organizeCustom.setOperId(webInput.getOperId());
		// 新增时间
		organizeCustom.setCreateTime(commonDaoImpl.getDatabaseSysTime());
		//日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建单位信息", "创建单位信息，单位名称 "+organizeCustom.getFullName());
		// 插入个人客户信息
		organizeCustomDaoImpl.insertOrganizeCustom(organizeCustom);
		return organizeId;
	}

	@Override
	public void updateOrganizeCustomInfo(PCPSWebInput webInput,
			OrganizeCustom organizeCustom) throws PCPSApplicationException {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL, "传入用户对象为空!");
		}
		OrganizeCustom _organizeCustom = organizeCustomDaoImpl
				.getOrganizeCustomByOrganizeId(organizeCustom.getOrganizeId());
		// 对象
		if (_organizeCustom == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL,
					"待修改单位信息数据库中不存在!");
		}
		// 对象赋值
		// _organizeCustom.setIssueCompanyNo(organizeCustom.getIssueCompanyNo());//
		// 发卡商NO
		_organizeCustom.setNationCodeId(organizeCustom.getNationCodeId());// 国家/地区编码
		_organizeCustom.setIndustryId(organizeCustom.getIndustryId());
		Date time = commonDaoImpl.getDatabaseSysTime();
		// 风险等级
		if (!PCPSUtil.isEmpty(organizeCustom.getRiskLevel())
				&& !organizeCustom.getRiskLevel().equals(
						_organizeCustom.getRiskLevel())) {
			
			RiskLevelChangeDetail rlcd = new RiskLevelChangeDetail();
			rlcd.setChangeId(commonDaoImpl.getUniqueIntegerKey());
			rlcd.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
			rlcd.setEntityType(RiskLevelChangeDetail.entityType_Organize);
			rlcd.setEntityID(organizeCustom.getOrganizeId().toString());
			rlcd.setChangeTime(time);
			rlcd.setOperId(webInput.getOperId());
			rlcd.setOrigRiskLevel(_organizeCustom.getRiskLevel());
			rlcd.setNewRiskLevel(organizeCustom.getRiskLevel());
			riskLevelChangeDetailDaoImpl.insert(rlcd);
		}
		_organizeCustom.setRiskLevel(organizeCustom.getRiskLevel());
		
		_organizeCustom.setFullName(organizeCustom.getFullName());// 单位全称
		_organizeCustom.setLegalPersonalId(organizeCustom.getLegalPersonalId());// 法人信息ID
		_organizeCustom.setAddress(organizeCustom.getAddress());// 单位地址
		_organizeCustom.setTelephone(organizeCustom.getTelephone());
		_organizeCustom.setFaxCode(organizeCustom.getFaxCode());
		_organizeCustom.setPostCode(organizeCustom.getPostCode());
		_organizeCustom.setRemark(organizeCustom.getRemark());
		// 修改操作员
		_organizeCustom.setLastModifyOperId(webInput.getOperId());
		// 修改时间
		_organizeCustom.setLastModifyTime(time);
		//日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "修改单位信息", "修改单位信息，单位名称 "+organizeCustom.getFullName());
		// 插入单位信息
		organizeCustomDaoImpl.updateOrganizeCustom(_organizeCustom);
	}
}
